home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / mac-emacs-src / prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-16  |  3.7 KB  |  167 lines  |  [TEXT/EMAC]

  1. /*
  2.  * Copyright (C) 1993, 1994 Marc Parmet.
  3.  * This file is part of the Macintosh port of GNU Emacs.
  4.  *
  5.  * GNU Emacs is distributed in the hope that it will be useful,
  6.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  8.  * GNU General Public License for more details.
  9.  */
  10.  
  11. #if defined(THINK_C)
  12. #include <MacHeaders>
  13. #else
  14. #include <Types.h>
  15. #include <Memory.h>
  16. #include <Quickdraw.h>
  17. #include <Windows.h>
  18. #include <Resources.h>
  19. #include <Errors.h>
  20. #endif
  21.  
  22. #include <Palettes.h>
  23. #include <Folders.h>
  24. #include <Script.h>
  25.  
  26. static int
  27. get_pref_file_spec(FSSpec *spec)
  28. {
  29.     short err;
  30.     short vRefNum;
  31.     long parID;
  32.     union {
  33.         unsigned char **uc;
  34.         Handle h;
  35.     } name;
  36.  
  37.     err = FindFolder(kOnSystemDisk,'pref',kCreateFolder,&vRefNum,&parID);
  38.     if (err) return err;
  39.     name.h = GetResource('STR ',131);
  40.     if (name.h == 0L) return resNotFound;
  41.     HLock(name.h);
  42.     err = FSMakeFSSpec(vRefNum,parID,*name.uc,spec);
  43.     HUnlock(name.h);
  44.     return err;
  45. }
  46.  
  47. static int
  48. create_pref_file(FSSpec *spec,int old_refNum,short *new_refNum)
  49. {
  50.     int i,err;
  51.     Handle h;
  52.     FInfo info;
  53.     static struct copyres {
  54.         long type;
  55.         short src,dst;
  56.     } copyres[] = {
  57.         'BNDL',129,128,
  58.         'FREF',132,132,
  59.         'EMAc',0,0,
  60.         'icl4',132,132,
  61.         'icl8',132,132,
  62.         'ICN#',132,132,
  63.         'ics#',132,132,
  64.         'ics4',132,132,
  65.         'ics8',132,132,
  66.         'STR ',132,-16397,
  67.         'vers',128,1,
  68.     };
  69.     int ncopyres = sizeof(copyres) / sizeof(struct copyres);
  70.  
  71.     FSpCreateResFile(spec,'EMAc','pref',smSystemScript);
  72.     if (err = ResError()) return err;
  73.     *new_refNum = FSpOpenResFile(spec,fsRdWrPerm);
  74.     if (err = ResError()) goto error;
  75.  
  76.     // Give the pref file an icon and a message for the user when trying to open.
  77.     for (i = 0; i<ncopyres; ++i) {
  78.         UseResFile(old_refNum);                                if (err = ResError()) goto error;
  79.         h = GetResource(copyres[i].type,copyres[i].src);    if (h == 0L) { err = resNotFound; goto error; }
  80.         DetachResource(h);                                    if (err = ResError()) goto error;
  81.         UseResFile(*new_refNum);                            if (err = ResError()) goto error;
  82.         AddResource(h,copyres[i].type,copyres[i].dst,(ConstStr255Param)"");
  83.                                                             if (err = ResError()) goto error;
  84.     }
  85.     
  86.     err = FSpGetFInfo(spec,&info);
  87.     if (err) goto error;
  88.     info.fdFlags |= fHasBundle;
  89.     err = FSpSetFInfo(spec,&info);
  90.     if (err) goto error;
  91.     return noErr;
  92.     
  93.     error:
  94.     FSpDelete(spec);
  95.     return err;
  96. }
  97.  
  98. // If pref is in file, set *h to a detached version of that resource.
  99. // Otherwise, *h is set to 0.
  100. int
  101. get_preference(int type,int n,Handle *h)
  102. {
  103.     int err;
  104.     short old_refNum,new_refNum;
  105.     FSSpec spec;
  106.  
  107.     old_refNum = CurResFile();
  108.     err = get_pref_file_spec(&spec);
  109.     if (err) { *h = 0L; return err; }
  110.     new_refNum = FSpOpenResFile(&spec,fsRdWrPerm);
  111.     if (err = ResError()) { *h = 0L; return err; }
  112.     if (type == 'pltt') {
  113.         *h = (Handle)GetNewPalette(n);
  114.         err = *h == 0L ? resNotFound : noErr;
  115.     }
  116.     else {
  117.         *h = Get1Resource(type,n);
  118.         if (*h != 0L) {
  119.             DetachResource(*h);
  120.             err = ResError();
  121.         }
  122.         else
  123.             err = resNotFound;
  124.     }
  125.     CloseResFile(new_refNum);
  126.     UseResFile(old_refNum);
  127.     if (err) *h = 0L;
  128.     return err;
  129. }
  130.  
  131. // Set preference n to data in h.  The caller
  132. // should not dispose of handle h after return.
  133. int
  134. set_preference(int type,int n,Handle h)
  135. {
  136.     int err;
  137.     short old_refNum,new_refNum;
  138.     FSSpec spec;
  139.     Handle k;
  140.  
  141.     old_refNum = CurResFile();
  142.     err = get_pref_file_spec(&spec);
  143.     switch (err) {
  144.     case fnfErr:
  145.         err = create_pref_file(&spec,old_refNum,&new_refNum);
  146.         break;
  147.     case noErr:
  148.         new_refNum = FSpOpenResFile(&spec,fsRdWrPerm);
  149.         err = ResError();
  150.         break;
  151.     default:
  152.         return err;
  153.     }
  154.     if (err) return err;
  155.  
  156.     SetResLoad(0);
  157.     k = Get1Resource(type,n);
  158.     SetResLoad(1);
  159.     if (k) RmveResource(k), DisposHandle(k);
  160.  
  161.     AddResource(h,type,n,(ConstStr255Param)"");
  162.     if ((err = ResError()) != noErr) return err;
  163.     CloseResFile(new_refNum);
  164.     UseResFile(old_refNum);
  165.     return noErr;
  166. }
  167.